package com.xh.model.request;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xh.model.enums.DeletedEnum;
import com.xh.model.table.Poster;
import com.xh.model.table.PosterItem;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PreviewRequest {

    private String name;

    private Long id;

    private int w;

    private int h;

    private String bgc;

    private String type;

    private int quality;

    private String bgUrl;

    private List<Items> items;

    private Editor editor;

    public Poster buildAdd() {
        LocalDateTime now = LocalDateTime.now();
        Poster posters = this.buildBaseAttributes(now);
        posters.setId(IdWorker.getId());
        posters.setCreateTime(now);
        posters.setDeleted(DeletedEnum.STATUS_0.getValue());
        return posters;
    }


    public Poster buildUpdate() {
        Poster posters = this.buildBaseAttributes(LocalDateTime.now());
        posters.setId(this.getId());
        return posters;
    }

    public List<PosterItem> buildPosterItems(PreviewRequest request, Poster poster) {
        return request.getItems().stream().map(item -> {
            PosterItem posterItem = new PosterItem();
            posterItem.setId(IdWorker.getId());
            posterItem.setT(item.getT());
            posterItem.setName(item.getName());
            posterItem.setUuid(item.getUuid());
            posterItem.setX(item.getX());
            posterItem.setY(item.getY());
            posterItem.setW(item.getW());
            posterItem.setH(item.getH());
            posterItem.setZ(item.getZ());
            posterItem.setS(item.getS());
            posterItem.setColor(item.getC());
            posterItem.setBgc(item.getBgc());
            posterItem.setV(item.getV());
            posterItem.setVd(item.getVd());
            posterItem.setFn(item.getFn());
            posterItem.setSt(item.getSt());
            posterItem.setActive(Boolean.TRUE.equals(item.getActive()) ? 1 : 0);
            posterItem.setPosterId(poster.getId());
            return posterItem;
        }).collect(Collectors.toList());
    }

    private Poster buildBaseAttributes(LocalDateTime now) {
        Poster posters = new Poster();
        posters.setName(this.getName());
        posters.setW(this.getW());
        posters.setH(this.getH());
        posters.setBgc(this.getBgc());
        posters.setType(this.getType());
        posters.setQuality(this.getQuality());
        posters.setBgUrl(this.getBgUrl());
        posters.setEditor(JSONUtil.toJsonStr(this.getEditor()));
        posters.setUpdateTime(now);
        return posters;
    }
}
